/* Copyright (c) 2004-2024 Peigen.info. All rights reserved. */

package info.peigen.hotpot.business.fee.core.service.processor.check;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSON;
import info.peigen.hotpot.business.fee.core.repository.entity.FeeRuleEntity;
import info.peigen.hotpot.business.fee.core.service.processor.base.AbstractFeeServiceProcessor;
import info.peigen.hotpot.business.fee.facade.command.order.rule.FeeRuleCreateOrder;
import info.peigen.hotpot.common.core.result.ResultCode;
import info.peigen.hotpot.common.service.base.exception.BusinessException;
import info.peigen.hotpot.common.service.base.processor.Processor;
import info.peigen.hotpot.common.service.base.processor.ProcessorServiceContext;
import org.beetl.sql.core.query.LambdaQuery;
import org.beetl.sql.core.query.Query;
import org.noear.solon.annotation.Component;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <b>(CheckDuplicateFeeRuleProcessor)</b>
 * 校验费用规则重复
 * 对于同一种费用场景，只能有唯一的一条规则能被创建，保证在业务计算费用规则时，有且只有一条规则被查到
 *
 * @author LiHai
 * @version 1.0.0
 * @since 2022/8/18
 */
@Component
public class CheckDuplicateFeeRuleProcessor extends AbstractFeeServiceProcessor implements Processor {
    @Override
    public Integer level() {
        return 19;
    }

    @Override
    public void execute(ProcessorServiceContext serviceContext) {
        FeeRuleCreateOrder order       = (FeeRuleCreateOrder) serviceContext.getOrder();
        boolean            partnerRule = StrUtil.isAllEmpty(order.getStationParentId(), order.getCustomerId());
        LambdaQuery<FeeRuleEntity> query = feeRuleRepository.getMapper().createLambdaQuery()
                .andEq(FeeRuleEntity::getCode, Query.filterNull(order.getCode()))
                .andEq(FeeRuleEntity::getMethod, Query.filterNull(order.getMethod()))
                .andEq(FeeRuleEntity::getCurrency, Query.filterNull(order.getCurrency()));

        if (partnerRule) {
            query.andIsNull(FeeRuleEntity::getStationParentId)
                    .andEq(FeeRuleEntity::getPartnerId, order.getPartnerId())
                    .andIsNull(FeeRuleEntity::getAccountId);
        } else {
            // 针对站点、个人账户的特殊规则
            if (StrUtil.isNotEmpty(order.getStationParentId())) {
                query.andEq(FeeRuleEntity::getStationParentId, Query.filterNull(order.getStationParentId()));
            }
            if (StrUtil.isNotEmpty(order.getPartnerId())) {
                query.andEq(FeeRuleEntity::getPartnerId, Query.filterNull(order.getPartnerId()));
            }
            if (StrUtil.isNotEmpty(order.getCustomerId())) {
                query.andEq(FeeRuleEntity::getAccountId, Query.filterNull(order.getCustomerId()));
            }
        }
        List<FeeRuleEntity> select = query.select();
        List<String>        names  = select.stream().map(FeeRuleEntity::getName).collect(Collectors.toList());
        Assert.isTrue(select.isEmpty(), () -> new BusinessException(ResultCode.FAILURE, partnerRule ? "重复的合作伙伴规则: " + JSON.toJSONString(names) : "重复的站点规则: " + JSON.toJSONString(names)));
    }

}